package guicomponents;

import gamecomponents.BigShip;
import gamecomponents.MediumShip;
import gamecomponents.MotherShip;
import gamecomponents.SmallShip;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Gui extends JFrame {

    private Menu menu = new Menu();
    private JPanel enemyPanel = new JPanel(),
            myPanel = new JPanel(),
            centerPanel = new JPanel(),
            southPanel = new JPanel(),
            statusPanel = new JPanel(),
            buttonSouthPanel = new JPanel();
    
    public static JButton mShip = new JButton("Mother ship"),
           bShip = new JButton("Big ship"),
           medShip = new JButton("Medium ship"),
           sShip = new JButton("Small ship");
    public static JLabel status = new JLabel("Welcome! Join to Server!");
    public static int selectedShip = 0;

    private Gui() {
        super("Torpedo");
        setBounds(000, 000, 1200, 900);

        setResizable(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        setVisible(true);
        buttonSetter(false);
        ItemHandler itemHandler = new ItemHandler();
        statusPanel.setBorder(BorderFactory.createTitledBorder("Instructions"));
        buttonSouthPanel.setBorder(BorderFactory.createTitledBorder("Choose a ship to add the field"));
        buttonSouthPanel.add(mShip);
        buttonSouthPanel.add(bShip);
        buttonSouthPanel.add(medShip);
        buttonSouthPanel.add(sShip);
        centerPanel.setLayout(new GridLayout(1, 2));
        centerPanel.add(myPanel);
        centerPanel.add(enemyPanel);
        southPanel.add(buttonSouthPanel, "West");
        statusPanel.add(status, "Center");
        southPanel.add(statusPanel, "Center");
        add(southPanel, "South");
        add(centerPanel, "Center");
        add(menu, "North");
        mShip.addActionListener(itemHandler);
        medShip.addActionListener(itemHandler);
        bShip.addActionListener(itemHandler);
        sShip.addActionListener(itemHandler);
        defineTheField(myPanel, enemyPanel);
    }

    private void defineTheField(JPanel my, JPanel enemy) {
        my.setLayout(new BorderLayout());
        enemy.setLayout(new BorderLayout());
        my.add(new BattleZone(false, 8, "Our Field"));
        enemy.add(new BattleZone(true, 8, "Enemy Field"));
    }

    public static void buttonSetter(boolean ENABLED) {
        mShip.setEnabled(ENABLED);
        bShip.setEnabled(ENABLED);
        medShip.setEnabled(ENABLED);
        sShip.setEnabled(ENABLED);
    }
    
    public static int getSelectedShip() {
        return selectedShip;
    }

    private class ItemHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == mShip) {
                mShip.setEnabled(false);
                selectedShip = 4; 
            }
            if (e.getSource() == bShip) {
                bShip.setEnabled(false);
                selectedShip = 3;
            }
            if (e.getSource() == medShip) {
                medShip.setEnabled(false);
                selectedShip = 2;
            }
            if (e.getSource() == sShip) {
                sShip.setEnabled(false);
                selectedShip = 1;
            }
        }
    }

    private static class GuiHolder {
        private final static Gui INSTANCE = new Gui();
    }

    public static Gui getInstance() {
        return GuiHolder.INSTANCE;
    }
}
